/*
 *    Copyright (c) 2018-2025, lengleng All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pie4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: lengleng (wangiegie@gmail.com)
 */

package com.pie4cloud.pie.bus.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pie4cloud.pie.bus.api.entity.DataKafConsumer;
import com.pie4cloud.pie.bus.service.KafkaManageService;
import com.pie4cloud.pie.common.core.util.R;
import com.pie4cloud.pie.common.log.annotation.SysLog;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;


/**
 * kafka消费者管理
 *
 * @author pie code generator
 * @date 2021-12-28 10:59:58
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/datakafconsumer" )
@Api(value = "datakafconsumer", tags = "kafka消费者管理管理")
public class KafkaMangerController {

    private final KafkaManageService kafkaManageService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param dataKafConsumer kafka消费者管理
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page" )
    @PreAuthorize("@pms.hasPermission('bus_datakafconsumer_get')" )
    public R getDataKafConsumerPage(Page page, DataKafConsumer dataKafConsumer) {
        return R.ok(kafkaManageService.pageAndMonitor(page,dataKafConsumer));
    }


    /**
     * 通过id查询kafka消费者管理
     * @param consId id
     * @return R
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/{consId}" )
    @PreAuthorize("@pms.hasPermission('bus_datakafconsumer_get')" )
    public R getById(@PathVariable("consId" ) Long consId) {
        return R.ok(kafkaManageService.getById(consId));
    }

    /**
     * 新增kafka消费者管理
     * @param dataKafConsumer kafka消费者管理
     * @return R
     */
    @ApiOperation(value = "新增kafka消费者管理", notes = "新增kafka消费者管理")
    @SysLog("新增kafka消费者管理" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('bus_datakafconsumer_add')" )
    public R save(@RequestBody DataKafConsumer dataKafConsumer) {
    	dataKafConsumer = kafkaManageService.saveAndSend(dataKafConsumer);
    	if(null!=dataKafConsumer&&null!=dataKafConsumer.getConsId()){
    		return R.ok(dataKafConsumer);
		}
        return R.failed();
    }

    /**
     * 修改kafka消费者管理
     * @param dataKafConsumer kafka消费者管理
     * @return R
     */
    @ApiOperation(value = "修改kafka消费者管理", notes = "修改kafka消费者管理")
    @SysLog("修改kafka消费者管理" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('bus_datakafconsumer_edit')" )
    public R updateById(@RequestBody DataKafConsumer dataKafConsumer) {
        return R.ok(kafkaManageService.editAndSend(dataKafConsumer));
    }

    /**
     * 通过id删除kafka消费者管理
     * @param consId id
     * @return R
     */
    @ApiOperation(value = "通过id删除kafka消费者管理", notes = "通过id删除kafka消费者管理")
    @SysLog("通过id删除kafka消费者管理" )
    @DeleteMapping("/{consId}" )
    @PreAuthorize("@pms.hasPermission('bus_datakafconsumer_del')" )
    public R removeById(@PathVariable Long consId) {
        return R.ok(kafkaManageService.delAndSend(consId));
    }


	@GetMapping(value = "/findThread")
	@ApiOperation(value = "获取所有线程", notes = "获取所有线程")
	public R<List<String>> findThread(){
		R<List<String>> resultT=new R<>();
		List<String> list=new ArrayList<>();
		ThreadGroup currentGroup = Thread.currentThread().getThreadGroup();
		int noThreads = currentGroup.activeCount();
		Thread[] lstThreads = new Thread[noThreads];
		currentGroup.enumerate(lstThreads);
		for (int i = 0; i < noThreads; i++){
			list.add("线程号：" + i + " = " + lstThreads[i].getName()+",线程状态="+ lstThreads[i].getState());

		}
		resultT.setData(list);
		return resultT;
	}
}
